home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / bench / RCS / proc.c,v < prev    next >
Text File  |  1989-02-22  |  11KB  |  418 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     89.02.22.11.37.25;  author jhh;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     89.02.21.11.15.08;  author brent;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @Routines to shapshot the process table during a benchmark
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @Now uses new Proc_GetPCBInfo and Vm_GetSegInfo interfaces
  28. @
  29. text
  30. @/* 
  31.  * proc.c --
  32.  *
  33.  *    Snapshot the process table before and after a benchmark in order
  34.  *    to account time spent in different processes and the kernel.
  35.  *
  36.  * Copyright 1989 Regents of the University of California
  37.  * Permission to use, copy, modify, and distribute this
  38.  * software and its documentation for any purpose and without
  39.  * fee is hereby granted, provided that the above copyright
  40.  * notice appear in all copies.  The University of California
  41.  * makes no representations about the suitability of this
  42.  * software for any purpose.  It is provided "as is" without
  43.  * express or implied warranty.
  44.  */
  45.  
  46. #ifndef lint
  47. static char rcsid[] = "$Header: /a/newcmds/bench/RCS/proc.c,v 1.1 89/02/21 11:15:08 brent Exp $ SPRITE (Berkeley)";
  48. #endif /* not lint */
  49.  
  50. #include <proc.h>
  51. #include <stdio.h>
  52.  
  53. void DoOneTime();
  54. void DoDeltaTime();
  55.  
  56.  
  57. /*
  58.  *----------------------------------------------------------------------
  59.  *
  60.  * GetProcTable --
  61.  *
  62.  *    Take a snapshot of the process table.
  63.  *
  64.  * Results:
  65.  *    The number of valid entries.
  66.  *
  67.  * Side effects:
  68.  *    Fills in the proc table that is passed in.
  69.  *
  70.  *----------------------------------------------------------------------
  71.  */
  72.  
  73. int
  74. GetProcTable(num, pcbs, argStrings)
  75.     int num;                /* Size of tables passed in */
  76.     Proc_PCBInfo *pcbs;            /* Array of PCBs */
  77.     Proc_PCBArgString *argStrings;    /* Array of argument strings */
  78. {
  79.     register int status;
  80.  
  81.     status = Proc_GetPCBInfo(0, num - 1, PROC_MY_HOSTID, sizeof(Proc_PCBInfo),
  82.                 pcbs, argStrings, &num);
  83.     if (status != SUCCESS) {
  84.     fprintf(stderr, "Couldn't read process table: %s\n",
  85.         Stat_GetMsg(status));
  86.     exit(1);
  87.     }
  88.     return(num);
  89. }
  90.  
  91. /*
  92.  *----------------------------------------------------------------------
  93.  *
  94.  * PrintProcStats --
  95.  *
  96.  *    Compute how much time has been spent by all existing processes
  97.  *    between two snapshots.
  98.  *
  99.  * Results:
  100.  *    None.
  101.  *
  102.  * Side effects:
  103.  *    Prints out info.
  104.  *
  105.  *----------------------------------------------------------------------
  106.  */
  107.  
  108. void
  109. PrintProcStats(outStream, numPCB1, pcbs1, argStrings1, numPCB2, pcbs2, argStrings2)
  110.     FILE *outStream;
  111.     int numPCB1;
  112.     Proc_PCBInfo *pcbs1;        /* Initial PCB snapshot */
  113.     Proc_PCBArgString *argStrings1;    /*     and argument strings */
  114.     int numPCB2;
  115.     Proc_PCBInfo *pcbs2;        /* Final PCB snapshot */
  116.     Proc_PCBArgString *argStrings2;    /*     and argument strings */
  117. {
  118.     register Proc_PCBInfo *pcb1Ptr;
  119.     register Proc_PCBInfo *pcb2Ptr;
  120.     register int i;
  121.     Time sumUserTime;
  122.     Time sumKernelTime;
  123.  
  124.     sumUserTime.seconds = 0;
  125.     sumUserTime.microseconds = 0;
  126.     sumKernelTime.seconds = 0;
  127.     sumKernelTime.microseconds = 0;
  128.     /*
  129.      * Loop through final snapshot.  For each entry either subtract
  130.      * out times indicated in the initial snapshot, or print out
  131.      * the total time because the process started after the benchmark did.
  132.      */
  133.     for (i=0, pcb1Ptr = pcbs1, pcb2Ptr = pcbs2 ;
  134.      i<numPCB2 ;
  135.      i++, pcb1Ptr++, pcb2Ptr++) {
  136. #ifdef notdef
  137.     /*
  138.      * HACK BECAUSE proc.h IS CHANGED.
  139.      */
  140.     if (i != 0) {
  141.         pcb1Ptr = (Proc_PCBInfo *)((int)pcb1Ptr - 5 * sizeof(int));
  142.         pcb2Ptr = (Proc_PCBInfo *)((int)pcb2Ptr - 5 * sizeof(int));
  143.     }
  144.     /* END HACK */
  145. #endif notdef
  146.     if (pcb2Ptr->state == PROC_UNUSED) {
  147.         continue;
  148.     }
  149.     if (NoTimeSpent(pcb2Ptr, pcb1Ptr)) {
  150.         continue;
  151.     }
  152.     if (i < numPCB1 && pcb2Ptr->processID == pcb1Ptr->processID) {
  153.         DoDeltaTime(outStream, "U",
  154.         &pcb2Ptr->userCpuUsage,
  155.         &pcb2Ptr->childUserCpuUsage,
  156.         &pcb1Ptr->userCpuUsage,
  157.         &pcb1Ptr->childUserCpuUsage,
  158.         &sumUserTime);
  159.         DoDeltaTime(outStream, "S",
  160.         &pcb2Ptr->kernelCpuUsage,
  161.         &pcb2Ptr->childKernelCpuUsage,
  162.         &pcb1Ptr->kernelCpuUsage,
  163.         &pcb1Ptr->childKernelCpuUsage,
  164.         &sumKernelTime);
  165.     } else {
  166.         DoOneTime(outStream, "U",
  167.         &pcb2Ptr->userCpuUsage,
  168.         &pcb2Ptr->childUserCpuUsage,
  169.         &sumUserTime);
  170.         DoOneTime(outStream, "S",
  171.         &pcb2Ptr->kernelCpuUsage,
  172.         &pcb2Ptr->childKernelCpuUsage,
  173.         &sumKernelTime);
  174.     }
  175.     if (strlen(argStrings2[i].argString) > 58) {
  176.         argStrings2[i].argString[58] = '\0';
  177.     }
  178.     fprintf(outStream, "%s\n", argStrings2[i].argString);
  179.     }
  180.     fprintf(outStream, "Total User %6d.%06d Total Kernel %6d.%06d\n",
  181.         sumUserTime.seconds, sumUserTime.microseconds,
  182.         sumKernelTime.seconds, sumKernelTime.microseconds);
  183. }
  184.  
  185. /*
  186.  *----------------------------------------------------------------------
  187.  *
  188.  * NoTimeSpent --
  189.  *
  190.  *    See if this process spent any time during the benchmark.
  191.  *
  192.  * Results:
  193.  *    TRUE if the process built up NO time.
  194.  *
  195.  * Side effects:
  196.  *    None.
  197.  *
  198.  *----------------------------------------------------------------------
  199.  */
  200. int
  201. NoTimeSpent(pcb2Ptr, pcb1Ptr)
  202.     register Proc_PCBInfo *pcb2Ptr;
  203.     register Proc_PCBInfo *pcb1Ptr;
  204. {
  205.     Time endTime, startTime;
  206.     if (pcb2Ptr->processID == pcb1Ptr->processID) {
  207.     Time_Add(pcb2Ptr->userCpuUsage, pcb2Ptr->childUserCpuUsage,
  208.         &endTime);
  209.     Time_Add(pcb1Ptr->userCpuUsage, pcb1Ptr->childUserCpuUsage,
  210.         &startTime);
  211.     Time_Subtract(endTime, startTime, &endTime);
  212.     if (endTime.seconds + endTime.microseconds != 0) {
  213.         return(0);    /* Have User CPU usage */
  214.     }
  215.     Time_Add(pcb2Ptr->kernelCpuUsage,
  216.         pcb2Ptr->childKernelCpuUsage, &endTime);
  217.     Time_Add(pcb1Ptr->kernelCpuUsage,
  218.         pcb1Ptr->childKernelCpuUsage, &startTime);
  219.     Time_Subtract(endTime, startTime, &endTime);
  220.     if (endTime.seconds + endTime.microseconds != 0) {
  221.         return(0);    /* Have Kernel CPU usage */
  222.     }
  223.     return(1);    /* No time */
  224.     } else {
  225.     Time_Add(pcb2Ptr->userCpuUsage, pcb2Ptr->childUserCpuUsage,
  226.         &endTime);
  227.     Time_Add(pcb2Ptr->kernelCpuUsage,
  228.         pcb2Ptr->childKernelCpuUsage, &startTime);
  229.     Time_Add(startTime, endTime, &endTime);
  230.     if (endTime.seconds + endTime.microseconds != 0) {
  231.         return(0);    /* Have CPU usage */
  232.     }
  233.     return(1);    /* No time */
  234.     }
  235. }
  236.  
  237. /*
  238.  *----------------------------------------------------------------------
  239.  *
  240.  * DoDeltaTime --
  241.  *
  242.  *    This computes the time spent in a process and its children
  243.  *    given the ending and starting times.  It prints the result
  244.  *    and accumulates it in a running total.
  245.  *
  246.  * Results:
  247.  *    Time time computed.
  248.  *
  249.  * Side effects:
  250.  *    Add the time into a total.
  251.  *
  252.  *----------------------------------------------------------------------
  253.  */
  254.  
  255. void
  256. DoDeltaTime(outStream, string, endTimePtr, endChildTimePtr,
  257.         startTimePtr, startChildTimePtr, totalTimePtr)
  258.     FILE *outStream;
  259.     char *string;
  260.     register Time *endTimePtr;
  261.     register Time *endChildTimePtr;
  262.     register Time *startTimePtr;
  263.     register Time *startChildTimePtr;
  264.     register Time *totalTimePtr;
  265. {
  266.     register int seconds;
  267.     register int microseconds;
  268.  
  269.     seconds = endTimePtr->seconds - startTimePtr->seconds +
  270.           endChildTimePtr->seconds - startChildTimePtr->seconds;
  271.     microseconds = endTimePtr->microseconds - startTimePtr->microseconds +
  272.          endChildTimePtr->microseconds - startChildTimePtr->microseconds;
  273.     while (microseconds < 0) {
  274.     microseconds += 1000000;
  275.     seconds -= 1;
  276.     }
  277.     fprintf(outStream, "%s %2d.%03d ", string, seconds, microseconds / 1000);
  278.     totalTimePtr->microseconds += microseconds;
  279.     if (totalTimePtr->microseconds > 1000000) {
  280.     totalTimePtr->microseconds -= 1000000;
  281.     seconds += 1;
  282.     }
  283.     totalTimePtr->seconds += seconds;
  284. }
  285.  
  286. /*
  287.  *----------------------------------------------------------------------
  288.  *
  289.  * DoOneTime --
  290.  *
  291.  *    Add a process's time to its children's time, then print the
  292.  *    result and accumulate it into a total.
  293.  *
  294.  * Results:
  295.  *    None.
  296.  *
  297.  * Side effects:
  298.  *    Accumulate the time in a total.
  299.  *
  300.  *----------------------------------------------------------------------
  301.  */
  302.  
  303. void
  304. DoOneTime(outStream, string, endTimePtr, endChildTimePtr, totalTimePtr)
  305.     FILE *outStream;
  306.     char *string;
  307.     register Time *endTimePtr;
  308.     register Time *endChildTimePtr;
  309.     register Time *totalTimePtr;
  310. {
  311.     register int seconds;
  312.     register int microseconds;
  313.  
  314.     seconds = endTimePtr->seconds + endChildTimePtr->seconds;
  315.     microseconds = endTimePtr->microseconds + endChildTimePtr->microseconds;
  316.     if (microseconds > 1000000) {
  317.     microseconds -= 1000000;
  318.     seconds += 1;
  319.     }
  320.     fprintf(outStream, "%s %2d.%03d ", string, seconds, microseconds / 1000);
  321.     totalTimePtr->microseconds += microseconds;
  322.     if (totalTimePtr->microseconds > 1000000) {
  323.     totalTimePtr->microseconds -= 1000000;
  324.     seconds += 1;
  325.     }
  326.     totalTimePtr->seconds += seconds;
  327. }
  328.  
  329. @
  330.  
  331.  
  332. 1.1
  333. log
  334. @Initial revision
  335. @
  336. text
  337. @d18 1
  338. a18 1
  339. static char rcsid[] = "$Header: /sprite/lib/forms/RCS/proto.c,v 1.2 89/01/07 04:12:18 rab Exp $ SPRITE (Berkeley)";
  340. d21 1
  341. a21 1
  342. #include <kernel/proc.h>
  343. d47 1
  344. a47 1
  345.     Proc_ControlBlock *pcbs;        /* Array of PCBs */
  346. d52 1
  347. a52 1
  348.     status = Proc_GetPCBInfo(0, num - 1, PROC_MY_HOSTID,
  349. d83 1
  350. a83 1
  351.     Proc_ControlBlock *pcbs1;        /* Initial PCB snapshot */
  352. d86 1
  353. a86 1
  354.     Proc_ControlBlock *pcbs2;        /* Final PCB snapshot */
  355. d89 2
  356. a90 2
  357.     register Proc_ControlBlock *pcb1Ptr;
  358.     register Proc_ControlBlock *pcb2Ptr;
  359. d107 1
  360. d112 2
  361. a113 2
  362.         pcb1Ptr = (Proc_ControlBlock *)((int)pcb1Ptr - 5 * sizeof(int));
  363.         pcb2Ptr = (Proc_ControlBlock *)((int)pcb2Ptr - 5 * sizeof(int));
  364. d116 1
  365. d125 4
  366. a128 4
  367.         &pcb2Ptr->userCpuUsage.time,
  368.         &pcb2Ptr->childUserCpuUsage.time,
  369.         &pcb1Ptr->userCpuUsage.time,
  370.         &pcb1Ptr->childUserCpuUsage.time,
  371. d131 4
  372. a134 4
  373.         &pcb2Ptr->kernelCpuUsage.time,
  374.         &pcb2Ptr->childKernelCpuUsage.time,
  375.         &pcb1Ptr->kernelCpuUsage.time,
  376.         &pcb1Ptr->childKernelCpuUsage.time,
  377. d138 2
  378. a139 2
  379.         &pcb2Ptr->userCpuUsage.time,
  380.         &pcb2Ptr->childUserCpuUsage.time,
  381. d142 2
  382. a143 2
  383.         &pcb2Ptr->kernelCpuUsage.time,
  384.         &pcb2Ptr->childKernelCpuUsage.time,
  385. d146 3
  386. a149 1
  387.     fprintf(outStream, "\n");
  388. d173 2
  389. a174 2
  390.     register Proc_ControlBlock *pcb2Ptr;
  391.     register Proc_ControlBlock *pcb1Ptr;
  392. d178 1
  393. a178 1
  394.     Time_Add(pcb2Ptr->userCpuUsage.time, pcb2Ptr->childUserCpuUsage.time,
  395. d180 1
  396. a180 1
  397.     Time_Add(pcb1Ptr->userCpuUsage.time, pcb1Ptr->childUserCpuUsage.time,
  398. d186 4
  399. a189 4
  400.     Time_Add(pcb2Ptr->kernelCpuUsage.time,
  401.         pcb2Ptr->childKernelCpuUsage.time, &endTime);
  402.     Time_Add(pcb1Ptr->kernelCpuUsage.time,
  403.         pcb1Ptr->childKernelCpuUsage.time, &startTime);
  404. d196 1
  405. a196 1
  406.     Time_Add(pcb2Ptr->userCpuUsage.time, pcb2Ptr->childUserCpuUsage.time,
  407. d198 2
  408. a199 2
  409.     Time_Add(pcb2Ptr->kernelCpuUsage.time,
  410.         pcb2Ptr->childKernelCpuUsage.time, &startTime);
  411. d248 1
  412. a248 1
  413.     fprintf(outStream, "%s %2d.%03d ", string, seconds, microseconds / 1000.);
  414. d291 1
  415. a291 1
  416.     fprintf(outStream, "%s %2d.%03d ", string, seconds, microseconds / 1000.);
  417. @
  418.